
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

/// 针对加载中 数据为空 数据加载失败做统一处理
class CommonViewStateHelper extends StatelessWidget {
  final ViewState model;
  final Color? textColors;
  final VoidCallback onEmptyPressed;
  final VoidCallback onErrorPressed;
  final VoidCallback onNoNetworkPressed;

  const CommonViewStateHelper(
        {Key? key,required this.model,
          this.textColors,
        required this.onEmptyPressed,
        required this.onErrorPressed,
        required this.onNoNetworkPressed}):super(key: key);

  @override
  Widget build(BuildContext context) {
    return const SizedBox();
  }
}

/// 初始化 加载中
class ViewStateLoadingWidget extends StatelessWidget {
  final double paddingTop;
  final Colors? textColors;
  const ViewStateLoadingWidget({Key? key, this.textColors,this.paddingTop=100}):super(key: key);
  @override
  Widget build(BuildContext context) {

    return  Container(
      alignment: Alignment.center,
      margin: EdgeInsets.only(top: paddingTop),
      child:const CupertinoActivityIndicator(),
    );
  }
}

/// 页面无数据
class ViewStateEmptyWidget extends StatelessWidget {
  final String? message;
  final Color? textColors;
  final String  helpTips;
  final VoidCallback? onPressed;
  final String img;
  final double paddingTop;
  const ViewStateEmptyWidget({Key? key,this.textColors, this.message, this.onPressed,this.img="",this.paddingTop=80,this.helpTips=""}):super(key:key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(top: paddingTop),
      child: GestureDetector(
          onTap: (){
            if(onPressed!=null){
              onPressed!();
            }
          },
          child: Column(
            children: [
              Image.asset(img.isEmpty?"assets/images/default_empty.png":img),
              const SizedBox(height: 20),
              Text(
                message ?? '暂无数据~',
                textAlign: TextAlign.center,
                style:  TextStyle(fontSize: 14, color:textColors??Colors.grey,fontWeight: FontWeight.w500),
              ),
              helpTips.isEmpty?Container():Column(
                children: [
                  const SizedBox(height: 10),
                  Text(
                    helpTips,
                    textAlign: TextAlign.center,
                    style:  TextStyle(fontSize: 12, color: textColors??Colors.grey),
                  ),
                ],
              )
            ],
          )
      ),
    );
  }
}

/// 页面数据请求异常
class ViewStateErrorWidget extends StatelessWidget {
  final ViewStateError? error;
  final VoidCallback? onPressed;
  final Color? textColors;
  final String img;
  final String message;
  final double paddingTop;
  const ViewStateErrorWidget({Key? key, this.textColors, this.error, this.onPressed,this.img="assets/images/load_error.png",this.paddingTop=80,this.message="服务器繁忙,点击重试"}):super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(top: paddingTop),
      child: GestureDetector(
          onTap: () {
            onPressed?.call();
          },
          child: Column(
            children: [
              img.isEmpty?const SizedBox():Image.asset(img),
              const SizedBox(height: 10),
              Text(
                message,
                textAlign: TextAlign.center,
                style:  TextStyle(fontSize: 16, color: textColors??Colors.grey),
              )
            ],
          )
      ),
    );
  }
}

/// 页面没有网络
class ViewStateNoNetworkWidget extends StatelessWidget {
  final String? message;
  final VoidCallback? onPressed;
  final Color? textColors;
  const ViewStateNoNetworkWidget({Key? key,this.textColors, this.message, this.onPressed}):super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GestureDetector(
          onTap: () {
            onPressed!();
          },
          child: Center(
            child: Container(
              padding: const EdgeInsets.all(20),
              child: Text(
                message ?? '没有网络,点击重试',
                textAlign: TextAlign.center,
                style:  TextStyle(fontSize: 16, color: textColors??Colors.grey),
              ),
            ),
          )),
    );
  }
}


/// 状态枚举类
enum ViewState {
  /// 加载中
  loading,

  /// 数据返回成功
  success,

  /// 空数据
  empty,

  /// 数据返回失败
  error,

  ///no net
  noNetwork,
}

class ViewStateError {
  Error error;
  String? message;

  ViewStateError({required this.error, this.message}) {
    message ??= '服务器异常';
  }
}